package ru.stepanovdn.trading.server.repository

import org.springframework.data.jpa.repository.Query
import org.springframework.data.jpa.repository.JpaRepository
import ru.stepanovdn.trading.server.entity.Candle
import java.time.OffsetDateTime

interface CandleRepository : JpaRepository<Candle?, Long?> {
    fun findByIntervalOrderByDateTime(interval: String): List<Candle>

    fun findByFigiAndIntervalOrderByDateTime(figi: String, interval: String): List<Candle>

    fun findByFigiAndIntervalAndDateTime(
        figi: String?,
        interval: String?,
        dateTime: OffsetDateTime?
    ): Candle?

    @Query(
        "select c from Candle c where c.figi = :figi AND c.interval = :interval " +
                "AND c.dateTime >= :startDateTime AND c.dateTime <= :endDateTime ORDER BY c.dateTime"
    )
    fun findByFigiAndIntervalAndBetweenDateTimes(
        figi: String?,
        interval: String?,
        startDateTime: OffsetDateTime?,
        endDateTime: OffsetDateTime?
    ): List<Candle>

    fun findByFigiAndIntervalAndDateTimeAfterOrderByDateTime(
        figi: String?,
        interval: String?,
        startDateTime: OffsetDateTime?
    ): List<Candle?>?

}
